package easy.验证回文串;


class Solution2 {
    public boolean isPalindrome(String s) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (Character.isLetterOrDigit(ch)) {
                stringBuilder.append(Character.toLowerCase(ch));
            }
        }
        int left = 0, right = stringBuilder.length() - 1;
        while (left < right) {
            //最中间的字符可以不判断
            if (stringBuilder.charAt(left) != stringBuilder.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }

    public static void main(String[] args) {
        String s = "A man, a plan, a canal: Panama";
        String s2 = "race a car";
        Solution2 solution = new Solution2();
        System.out.println(solution.isPalindrome(s));
        System.out.println(solution.isPalindrome(s2));
    }
}